/**
 * 您的公司.com
 * Copyright (c) 2018-2018 All Rights Reserved.
 */
package com.mei.demo.service.impl;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import com.mei.demo.service.AsyncDemoService;

/**
 * 
 *
 * @author mei.xiaoliang@qq.com
 */
@Service
public class AsyncDemoServiceImpl implements AsyncDemoService {

    //    private static final Logger log = LoggerFactory.getLogger(AsyncDemo.class);

    /**
     * 最简单的异步调用，返回值为void
     * @throws InterruptedException 
     */
    @Override
    @Async
    public void asyncInvokeSimplest() throws InterruptedException {
        System.out.println("asyncSimplest start");
        for (int i = 0; i < 200; i++) {
            System.out.println(22222222);
        }
        System.out.println("asyncInvokeSimplest end");
    }

    /**
     * 带参数的异步调用 异步方法可以传入参数
     * 
     * @param s
     */
    @Override
    @Async
    public void asyncInvokeWithParameter(String s) {
        System.out.println("asyncInvokeWithParameter: s = " + s);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        System.out.println("asyncInvokeWithParameter 3s ...");
    }

    /**
     * 异常调用返回Future
     * 
     * @param i
     * @return
     */
    @Override
    @Async
    public Future<String> asyncInvokeReturnFuture(int i) {
        System.out.println("asyncInvokeReturnFuture: i = " + i);
        Future<String> future;
        try {
            Thread.sleep(1000 * 1);
            future = new AsyncResult<String>("success:" + i);
        } catch (InterruptedException e) {
            future = new AsyncResult<String>("error");
        }
        return future;
    }

}
